package org.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 要求使用JDBC在tedu库下的user表中插入1条记录
 */
public class Demo01JDBC {
    /**
     * 1 加载数据库驱动
     * 2 创建数据库连接对象
     * 3 创建SQL语句的执行对象
     * 4 执行SQL语句
     * 5 获取SQL语句执行结果
     * 6 断开数据库连接
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true", "root", "root");
        Statement statement = connection.createStatement();
//        接收用户数据
        Scanner scan =new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scan.nextLine();
        System.out.println("请输入密码：");
        String password = scan.nextLine();
        System.out.println("请输入昵称：");
        String nickname=scan.nextLine();
        System.out.println("请输入年龄：");
        int age= scan.nextInt();
        statement.executeUpdate("INSERT INTO user(username,password,nickname,age) VALUES ('"+username+"','"+password+"','"+nickname+"',"+age+")");
        System.out.println("数据插入成功");
        connection.close();
    }
}
